#!/usr/bin/env python

#   
#     t_newsrc.py - Test script for newsrc.py
#
#  Copyright (c) 2007, Alex Marandon
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
#  
#    * Redistributions of source code must retain the above copyright notice,
#      this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright notice,
#      this list of conditions and the following disclaimer in the documentation
#      and/or other materials provided with the distribution.
#    * Neither the name of newsrc.py nor the names of its contributors
#      may be used to endorse or promote products derived from this software 
#      without specific prior written permission.
#  
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGE.

import sys
sys.path.append('..')
from newsrc import Newsrc, Articles
import os.path
import unittest

class TestArticles(unittest.TestCase):

    test_str = '1,7,10-16,40,51-67'
    ref_list = [1, 7, 10, 11, 12, 13, 14, 15, 16, 40, 51, 52, 53, 54, 55, 
        56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67]

    def testList(self):
        articles = Articles(self.test_str)
        self.assertEqual(articles.getList(), self.ref_list)

    def testRemove(self):
        articles = Articles(self.test_str)
        self.assertTrue(articles.remove((1,13, 55, 67)))
        self.assertEqual(articles.getString(), '7,10-12,14-16,40,51-54,56-66')

    def testAdd(self):
        articles = Articles(self.test_str)
        articles.add((2, 3 , 41, 42, 43, 49, 50, 68, 112))
        self.assertEqual(articles.getString(), '1-3,7,10-16,40-43,49-68,112')

class TestNewsrc(unittest.TestCase):

    file1 = 'newsrc_example_1'

    def testLoadFile(self):
        newsrc = Newsrc(self.file1)
        for group in newsrc.groups:
            if group.name == 'fr.rec.toasters':
                self.assertEqual(group.subscribed, '!')

    def testExportRc(self):
        newsrc = Newsrc(self.file1)
        self.assertEqual(newsrc.export_rc(), 
                ['alt.toto: 1-2,7,42-220,300,303,305-2700', 
                 'fr.vacuum-cleaners: 10-88,91,400-502,600', 
                 'fr.rec.toasters! 10-543,600'])

    def testSaveAs(self):
        newsrc = Newsrc(self.file1)
        new_file_name = 'test_save_as_file'
        old_file_name = new_file_name + '.bak'

        # file doesn't exist yet, no backup
        newsrc.saveAs(new_file_name)
        self.assertTrue(os.path.isfile(new_file_name))
        self.assertFalse(os.path.isfile(old_file_name))

        # file exists, backup needed
        newsrc.saveAs(new_file_name)
        self.assertTrue(os.path.isfile(new_file_name))
        self.assertTrue(os.path.isfile(old_file_name))

        os.remove(new_file_name)
        os.remove(old_file_name)

    def testHasGroup(self):
        newsrc = Newsrc(self.file1)
        self.assertTrue(newsrc.hasGroup('alt.toto'))
        self.assertFalse(newsrc.hasGroup('foobar'))

    def testDelGroup(self):
        newsrc = Newsrc(self.file1)
        self.assertTrue(newsrc.delGroup('alt.toto'))
        self.assertFalse(newsrc.delGroup('foobar'))
        self.assertFalse(newsrc.hasGroup('alt.toto'))
        
    def testAddGroup(self):
        newsrc = Newsrc(self.file1)
        self.assertTrue(newsrc.addGroup('alt.foo.bar'))
        self.assertTrue(newsrc.hasGroup('alt.foo.bar'))
        self.assertFalse(newsrc.addGroup('alt.foo.bar'))
        self.assertTrue(newsrc.addGroup('alt.foo.bar', True))
        self.assertTrue(newsrc.hasGroup('alt.foo.bar'))

    def testSubscribe(self):
        newsrc = Newsrc(self.file1)
        self.assertFalse(newsrc.isSubscribed('fr.rec.toasters'))
        newsrc.subscribe('fr.rec.toasters')
        self.assertTrue(newsrc.isSubscribed('fr.rec.toasters'))
        newsrc.unsubscribe('fr.rec.toasters')
        self.assertFalse(newsrc.isSubscribed('fr.rec.toasters'))

    def testMark(self):
        newsrc = Newsrc(self.file1)
        newsrc.mark('fr.rec.toasters', 2006)
        self.assertTrue(newsrc.isMarked('fr.rec.toasters', 2006))
        newsrc.markList('fr.rec.toasters', (2042, 2043))
        self.assertTrue(newsrc.isMarked('fr.rec.toasters', 2042))
        self.assertTrue(newsrc.isMarked('fr.rec.toasters', 2043))
        self.assertFalse(newsrc.isMarked('fr.rec.toasters', 20422))

    def testUnmark(self):
        newsrc = Newsrc(self.file1)
        group_name = 'alt.toto'
        self.assertTrue(newsrc.isMarked(group_name, 213))
        self.assertFalse(newsrc.isMarked(group_name, 223))
        self.assertTrue(newsrc.unMark(group_name, 213))
        self.assertFalse(newsrc.unMark(group_name, 223))
        self.assertFalse(newsrc.isMarked(group_name, 213))
        newsrc.unMarkList(group_name, (300, 2000))
        self.assertFalse(newsrc.isMarked(group_name, 300))
        self.assertFalse(newsrc.isMarked(group_name, 2000))

    def testNoFile(self):
        temp_newsrc = 'temp_newsrc'
        newsrc = Newsrc()
        self.assertTrue(newsrc.addGroup('ca.jobs'))
        self.assertTrue(newsrc.addGroup('ca.rec.sports.hockey'))
        newsrc.saveAs(temp_newsrc)
        newsrc2 = Newsrc(temp_newsrc) 
        self.assertTrue(newsrc2.hasGroup('ca.jobs'))
        self.assertTrue(newsrc2.hasGroup('ca.rec.sports.hockey'))
        os.remove(temp_newsrc)

    def testMarkRange(self):
        temp_newsrc = 'temp_newsrc'
        newsrc = Newsrc()
        self.assertTrue(newsrc.addGroup('ca.jobs'))
        self.assertTrue(newsrc.addGroup('ca.rec.sports.hockey'))
        newsrc.markRange('ca.jobs', 1, 42)
        newsrc.unMarkRange('ca.jobs', 12, 22)
        newsrc.markRange('ca.jobs', 50, 4000)
        newsrc.markRange('ca.rec.sports.hockey', 3000000, 3005000)
        newsrc.unMarkRange('ca.rec.sports.hockey', 3001000, 3002000)
        newsrc.saveAs(temp_newsrc)
        newsrc2 = Newsrc(temp_newsrc) 
        self.assertTrue(newsrc2.isMarked('ca.jobs', 1))
        self.assertFalse(newsrc2.isMarked('ca.jobs', 12))
        self.assertFalse(newsrc2.isMarked('ca.jobs', 22))
        self.assertTrue(newsrc2.isMarked('ca.jobs', 23))
        self.assertTrue(newsrc2.isMarked('ca.jobs', 42))
        self.assertTrue(newsrc2.isMarked('ca.rec.sports.hockey', 3000000))
        self.assertTrue(newsrc2.isMarked('ca.rec.sports.hockey', 3003984))
        self.assertFalse(newsrc2.isMarked('ca.rec.sports.hockey', 3001000))
        self.assertFalse(newsrc2.isMarked('ca.rec.sports.hockey', 3001333))
        self.assertFalse(newsrc2.isMarked('ca.rec.sports.hockey', 3002000))
        self.assertTrue(newsrc2.isMarked('ca.rec.sports.hockey', 3005000))
        os.remove(temp_newsrc)

    def testDoubleMark(self):
        temp_newsrc = 'temp_newsrc'
        newsrc = Newsrc()
        self.assertTrue(newsrc.addGroup('ca.jobs'))
        newsrc.markRange('ca.jobs', 1, 42)
        newsrc.saveAs(temp_newsrc)
        newsrc2 = Newsrc(temp_newsrc) 
        newsrc.markRange('ca.jobs', 1, 42)
        newsrc.save()
        newsrc3 = Newsrc(temp_newsrc) 
        self.assertTrue(newsrc3.isMarked('ca.jobs', 1))
        self.assertTrue(newsrc3.isMarked('ca.jobs', 13))
        self.assertTrue(newsrc3.isMarked('ca.jobs', 42))
        os.remove(temp_newsrc)

if __name__ == '__main__':
    unittest.main()

#EOF
